<?php

ob_start();
session_start();
include "checksession.php";
include_once "../connect.php";

$emp_id = $_POST['Emp_id'];
$c_keyboard = $_POST['c_keyboard'];
$c_monitor = $_POST['c_monitor'];
$c_mouse = $_POST['c_mouse'];
$cn_number = $_POST['cn_number'];
$cn_id = $_POST['Cn_id'];
$c_id = $_POST['C_id'];
$oth_id = $_POST['Oth_id'];


$sqlCnNumber = " select * from comname where cn_number = '$cn_number' and cn_id != '$cn_id' ";
$CN_Number = mysql_query($sqlCnNumber);
if (mysql_num_rows($CN_Number) != 0) {
    echo "	<script language='javascript'>
		alert('ชื่อเครื่องซ้ำครับ');
		setTimeout(\"parent.$.fancybox.close()\",1000);
		</script>";
    exit();
}

if(!empty($cn_id)){
 $strSQL = "SELECT  *  FROM comname LEFT  JOIN depart ON comname.de_id = depart.de_id
                                                         LEFT  JOIN computer ON comname.cn_id = computer.cn_id
                                                         LEFT  JOIN comother ON comname.cn_id = comother.cn_id
                                                         WHERE comname.cn_id = '$cn_id'";
                                                             
 $rsComOt = mysql_query($strSQL);
 $rExeComputer = mysql_fetch_array($rsComOt);
 
 $cnNumber = $rExeComputer['cn_number'];
 $keyBoad = $rExeComputer['c_keyboard'];
 $mouse = $rExeComputer['c_mouse'];
 $monitor = $rExeComputer['c_monitor'];

 $upDateProDe1 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_code = '$cnNumber'";
 mysql_query($upDateProDe1);
                                
 $upDateProDe2 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_code = '$keyBoad'";
 mysql_query($upDateProDe2);
 
 $upDateProDe3 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_code = '$mouse'";
 mysql_query($upDateProDe3);
 
 $upDateProDe4 = "UPDATE product_detail SET pd_status = 'donate' WHERE pd_code = '$monitor'";
 mysql_query($upDateProDe4);
}
                                                         
if (!empty($cn_id)) {
    $sql1 = "
UPDATE  comname  SET
cn_number = '$cn_number',
     cn_createdate =  NOW()
 WHERE cn_id = '$cn_id' ";


    $sql2 = "
UPDATE `db_itmanager`.`computer` SET 
c_keyboard = '$c_keyboard',
c_mouse = '$c_mouse',
c_monitor = '$c_monitor',
cn_number  = '$cn_number'
    WHERE `computer`.`c_id` ='$c_id' ";
    


    $sql3 = "
    UPDATE `db_itmanager`.`comother` SET
   cn_number = '$cn_number'
WHERE `comother`.`oth_id` ='$oth_id' ";
 

    } else {
    echo mysql_error();
}
if (mysql_query($sql1)) {
    
}if (mysql_query($sql2)) {
    
}if (mysql_query($sql3)) {
    
   
    // header("location:listCom.php");
  $upKey = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_code` ='$c_keyboard'";
        mysql_query($upKey);

        $upMou = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_code` ='$c_mouse'";
        mysql_query($upMou);

        $upMoni = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_code` ='$c_monitor'";
        mysql_query($upMoni);

        $upCom = "UPDATE  `db_itmanager`.`product_detail` SET  `emp_id` =  '$emp_id',
`pd_status` =  'IsUse', pd_outDate = NOW()  WHERE  `product_detail`.`pd_code` ='$cn_number'";
        mysql_query($upCom);

        $exeCom = "SELECT * FROM product_detail WHERE pd_code = '$cn_number'";
        $rs = mysql_query($exeCom);

        if ($rs) {

            $r = mysql_fetch_array($rs);
            $Prd_id = $r['prd_id'];

            $exeComDe = "SELECT * FROM product WHERE prd_id = '$Prd_id'";
            $rsComD = mysql_query($exeComDe);

            $rComDe = mysql_fetch_array($rsComD);
            $Prd_balance = $rComDe['prd_balance'];

            $upComDe = "UPDATE product SET prd_balance = '$Prd_balance' - '1' WHERE prd_id = '$Prd_id'  ";

            mysql_query($upComDe);
        } else {
            echo mysql_error();
        }
        $exeMoniDe = "SELECT * FROM product_detail WHERE pd_code = '$c_monitor'";
        $rsMoniDe = mysql_query($exeMoniDe);

        if ($rsMoniDe) {
            $rMoniDe = mysql_fetch_array($rsMoniDe);
            $Prd_idMoniDe = $rMoniDe['prd_id'];

            $exeMoni = "SELECT * FROM product WHERE prd_id = '$Prd_idMoniDe'";
            $rsMoni = mysql_query($exeMoni);
            $rMoni = mysql_fetch_array($rsMoni);
            $Prd_balanceMoni = $rMoni['prd_balance'];

            $upMoniBalan = "UPDATE product SET prd_balance = '$Prd_balanceMoni' - '1' WHERE prd_id = '$Prd_idMoniDe'";

            mysql_query($upMoniBalan);
        } else {
            echo mysql_error();
        }

        $exeMousDe = "SELECT * FROM product_detail WHERE pd_code = '$c_mouse'";
        $rsMouseDe = mysql_query($exeMousDe);
        if ($rsMouseDe) {
            $rMousDe = mysql_fetch_array($rsMouseDe);
            $prd_idMouse = $rMousDe['prd_id'];

            $exeMouse = "SELECT * FROM product WHERE prd_id = '$prd_idMouse' ";
            $rsMouse = mysql_query($exeMouse);
            $rMouse = mysql_fetch_array($rsMouse);
            $Prd_balanceMou = $rMouse['prd_balance'];

            $upMouseBal = "UPDATE  product SET prd_balance = '$Prd_balanceMou' - '1' WHERE prd_id = '$prd_idMouse'";
            mysql_query($upMouseBal);
        } else {
            echo mysql_error();
        }

        $exeKeyDe = "SELECT * FROM product_detail WHERE pd_code = '$c_keyboard'";
        $rsKeyDe = mysql_query($exeKeyDe);
        if ($rsKeyDe) {
            $rKeyDe = mysql_fetch_array($rsKeyDe);
            $Prd_idKey = $rKeyDe['prd_id'];

            $exeKey = "SELECT * FROM product WHERE prd_id = '$Prd_idKey'";
            $rsKey = mysql_query($exeKey);
            $rKey = mysql_fetch_array($rsKey);
            $Prd_balKey = $rKey['prd_balance'];

            $upKey = "UPDATE product SET prd_balance = '$Prd_balKey' - '1' WHERE prd_id = '$Prd_idKey' ";
            mysql_query($upKey);
        } else {
            echo mysql_error();
        }
        
         echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
    echo "<meta http-equiv='refresh' content='5; url=listCom.php'>";
} else {
    echo mysql_error();
}
?>


        


